<template>
  <div class="app-container">
    <!-- 公司基本信息 -->
    <el-row>
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>公司基本信息</span>
        </div>
        <div style="width: 100%">
          <el-form ref="ProjectForm" :model="companyInfo" :rules="companyInfoRules" label-position="right" label-width="100px">
            <el-form-item label="公司名称" prop="name">
              <el-input v-model="companyInfo.name" placeholder="请输入公司名称" />
            </el-form-item>
          </el-form>
          <div style="text-align: center">
            <el-button v-if="checkPermission('/manage/company/update')" type="primary" @click="updateCompanyInfo()">保 存</el-button>
          </div>
        </div>
      </el-card>
    </el-row>

    <!-- 公司 - 项目 列表 -->
    <el-row style="margin-top: 50px;">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>公司 - 项目 列表</span>
        </div>
        <div style="text-align: right">
          <el-button-group style="margin-bottom: 4px;">
            <el-tooltip v-permission="'/manage/project/add'" :open-delay="500" content="添加项目" placement="top">
              <el-button icon-class="skill" size="mini" type="primary" @click="handleProjectCreate()"><i class="el-icon-plus" /></el-button>
            </el-tooltip>
          </el-button-group>
        </div>
        <el-table v-loading="listLoading" :data="projectListData" border element-loading-text="Loading" fit highlight-current-row>
          <el-table-column align="center" label="ID" prop="id" sortable width="95">
            <template v-slot="{row}">
              {{ row.id }}
            </template>
          </el-table-column>
          <el-table-column label="项目名称" prop="name" sortable width="250">
            <template v-slot="{row}">
              {{ row.name }}
            </template>
          </el-table-column>
          <el-table-column label="项目描述" prop="name">
            <template v-slot="{row}">
              {{ row.desc }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="创建时间" prop="created_at" width="200">
            <template v-slot="{row}">
              <span> {{ row.createdAt | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }} </span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="100px">
            <template v-slot="{row}">
              <el-button v-permission="'/manage/project/update'" size="small" type="primary" @click="handleProjectUpdate(row)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </el-row>

    <!-- 项目信息弹窗 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="projectDialogFormVisible" width="800px" :close-on-click-modal="false">
      <el-form ref="projectDataForm" :model="projectTemp" :rules="projectInfoRules" label-position="right" label-width="110px">
        <el-form-item label="项目名称" prop="name">
          <el-input v-model="projectTemp.name" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目描述" prop="desc">
          <el-input v-model="projectTemp.desc" placeholder="请输入项目描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="projectDialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createProject():updateProject()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission' // 权限判断函数
import { update as companyUpdate, getUserCompanyInfo } from '@/api/manage/company'
import { update as projectUpdate, create as projectCreate } from '@/api/manage/project'

export default {
  data() {
    return {
      listLoading: false,
      projectListData: [],
      projectDialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '修改',
        create: '添加'
      },
      projectTemp: {
        name: ''
      },
      companyInfo: {},
      projectInfoRules: {
        name: [
          { required: true, message: '请输入项目名称', trigger: 'change' },
          { max: 50, message: '项目名称 长度不能超过 50 个字符', trigger: 'blur' }
        ],
        desc: [
          { max: 255, message: '项目描述 长度不能超过 255 个字符', trigger: 'blur' }
        ]
      },
      companyInfoRules: {
        name: [
          { required: true, message: '请输入公司名称', trigger: 'change' },
          { max: 50, message: '公司名称 长度不能超过 50 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getCompanyInfo()
  },
  methods: {
    checkPermission,
    // 点击添加项目按钮
    handleProjectCreate() {
      this.projectTemp = {
        id: undefined,
        companyId: this.companyInfo.id,
        name: '',
        desc: '',
        mock: false,
        mockPreUrl: null
      }
      this.dialogStatus = 'create'
      this.projectDialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['projectDataForm'].clearValidate()
      })
    },
    // 点击修改项目按钮
    handleProjectUpdate(row) {
      this.projectTemp = Object.assign({}, row) // 复制行数据
      this.dialogStatus = 'update'
      this.projectDialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['projectDataForm'].clearValidate()
      })
    },
    // 添加项目信息
    createProject() {
      this.$refs['projectDataForm'].validate((valid) => {
        if (valid) {
          projectCreate(this.projectTemp).then(() => {
            this.projectDialogFormVisible = false
            this.$message({ message: '添加成功!', type: 'success' })
            this.getCompanyInfo()
          })
        }
      })
    },
    // 更新项目信息
    updateProject() {
      this.$refs['projectDataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.projectTemp)
          projectUpdate(tempData).then(() => {
            const index = this.projectListData.findIndex(v => v.id === this.projectTemp.id)
            this.projectListData.splice(index, 1, this.projectTemp)
            this.projectDialogFormVisible = false
            this.$message({ message: '修改成功!', type: 'success' })
          })
        }
      })
    },
    // 获得用户所属公司信息
    getCompanyInfo() {
      this.listLoading = true
      getUserCompanyInfo().then(response => {
        this.companyInfo = { id: response.data.id, name: response.data.name }
        this.projectListData = response.data.projectList
        this.listLoading = false
      })
    },
    // 更新公司信息
    updateCompanyInfo() {
      this.$refs['ProjectForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.companyInfo)
          companyUpdate(tempData).then(() => {
            this.$message({ message: '修改成功!', type: 'success' })
          })
        }
      })
    }
  }
}
</script>
